package org.gntc.prayer.myprayer;

import java.util.LinkedHashMap;
import java.util.Map;

import org.gntc.prayer.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

public class PrayerListAdapter extends BaseAdapter
{
	public final static int TYPE_SECTION_HEADER = 0;
	
	public final ArrayAdapter<String> m_headers;
	public final Map<String, Adapter> m_sections; 
	
	/**
	 * 생성자
	 * @param context
	 */
	public PrayerListAdapter(Context context)
	{
		m_headers = new ArrayAdapter<String>(context, R.layout.list_header);
		m_sections = new LinkedHashMap<String, Adapter>();
	}
	
	/**
	 * Add section
	 * @param section
	 * @param adapter
	 */
	public void addSection(String strHeader, Adapter adapter)
	{
		this.m_headers.add(strHeader);
		this.m_sections.put(strHeader, adapter);
	}
	
	/**
	 * Get count of adapter
	 */
	public int getCount()
	{
		int nTotal = 0;
		
		for (Adapter adapter : this.m_sections.values())
		{
			nTotal += adapter.getCount() + 1;
		}
		
		return nTotal;
	}
	
	/**
	 * Get view type count
	 */
	public int getViewTypeCount()
	{
		int nTotal = 1;
		
		for (Adapter adapter : this.m_sections.values())
		{
			nTotal += adapter.getViewTypeCount();
		}
		
		return nTotal;
	}

	/**
	 * Get item
	 */
	public Object getItem(int position)
	{
		for (Object key : this.m_sections.keySet())
		{
			Adapter adapter = m_sections.get(key);
			int size = adapter.getCount() + 1;
			
			if (position == 0)
			{
				return key;
			}
			
			if (position < size)
			{
				return adapter.getItem(position - 1);
			}
			
			position -= size;
		}
		
		return null;
	}
	
	/**
	 * Get item view type
	 */
	public int getItemViewType(int position)
	{
		int type = 1;
		
		for (Object key : this.m_sections.keySet())
		{
			Adapter adapter = m_sections.get(key);
			int size = adapter.getCount() + 1;
			
			if (position == 0)
			{
				return TYPE_SECTION_HEADER;
				//return type + adapter.getItemViewType(position - 1);
			}
			
			if (position < size)
			{
				return type + adapter.getItemViewType(position - 1);
			}
			
			position -= size;
			type += adapter.getViewTypeCount();
		}
		
		return -1;
	}

	public boolean areAllItemsSelectable()
	{
		return false;
	}
	
	public boolean isEnabled(int position)
	{
		return (getItemViewType(position) != TYPE_SECTION_HEADER);
	}

	/**
	 * Get item ID
	 */
	public long getItemId(int position)
	{
		for (Object key : this.m_sections.keySet())
		{
			Adapter adapter = m_sections.get(key);
			int size = adapter.getCount() + 1;
			
			if (position == 0)
			{
				return TYPE_SECTION_HEADER;
			}
			
			if (position < size)
			{
				String strItem = adapter.getItem(position - 1).toString();
				String[] items = strItem.split(",");
				String[] values = items[0].split("=");
				long lID = Integer.parseInt(values[1]);
				return lID;
			}
			
			position -= size;
		}
		
		return 1;
	}

	/**
	 * Get view
	 */
	public View getView(int position, View convertView, ViewGroup parent)
	{
		int sectionnum = 0;
		
		for (Object key : this.m_sections.keySet())
		{
			Adapter adapter = m_sections.get(key);
			int size = adapter.getCount() + 1;
			
			if (position == 0)
			{
				return m_headers.getView(sectionnum, convertView, parent);
			}
			
			if (position < size)
			{
				return adapter.getView(position - 1, convertView, parent);
			}
			
			position -= size;
			sectionnum++;
		}
		
		return null;
	}
	
}
